package asynq

import (
	"context"
	"github.com/hibiken/asynq"
)

var AsynqRedisClineOpt *asynq.RedisClientOpt
var asynqService *asynq.ServeMux
var srv *asynq.Server

func SetAsynqRedisClineOpt(addr string, DB int) {
	AsynqRedisClineOpt = &asynq.RedisClientOpt{
		Addr: addr,
		DB:   DB,
	}

	r := AsynqRedisClineOpt
	srv = asynq.NewServer(*r, asynq.Config{
		Concurrency: 1,
	})
	asynqService = asynq.NewServeMux()
	// 中间件 记录
	asynqService.Use(loggingMiddleware)
}

func AsynqAddWork(pattern string, handler func(context.Context, *asynq.Task) error) {
	asynqService.HandleFunc(pattern, handler)
}

func AsynqStartWork() error {

	if err := srv.Run(asynqService); err != nil {
		return err
	} else {
		return nil
	}
}
